/*     */ package com.zimbra.cs.dav.service.method;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavException.CannotModifyProtectedProperty;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import com.zimbra.cs.dav.service.DavMethod;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import java.io.IOException;
/*     */ import java.util.Collection;
/*     */ import java.util.List;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PropPatch
/*     */   extends DavMethod
/*     */ {
/*     */   public static final String PROPPATCH = "PROPPATCH";
/*     */   
/*     */   public String getName()
/*     */   {
/*  44 */     return "PROPPATCH";
/*     */   }
/*     */   
/*     */   public void handle(DavContext ctxt) throws DavException, IOException, ServiceException
/*     */   {
/*  49 */     if (!ctxt.hasRequestMessage()) {
/*  50 */       throw new DavException("empty request", 400);
/*     */     }
/*     */     
/*  53 */     Document req = ctxt.getRequestMessage();
/*  54 */     Element top = req.getRootElement();
/*  55 */     if (!top.getName().equals("propertyupdate"))
/*  56 */       throw new DavException("msg " + top.getName() + " not allowed in PROPPATCH", 400, null);
/*  57 */     DavResource resource = ctxt.getRequestedResource();
/*  58 */     handlePropertyUpdate(ctxt, top, resource, false, "PROPPATCH");
/*  59 */     DavResponse resp = ctxt.getDavResponse();
/*     */     
/*  61 */     resp.addResource(ctxt, resource, ctxt.getResponseProp(), false);
/*  62 */     sendResponse(ctxt);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Pair<List<Element>, List<Element>> getSetsAndRemoves(Element top, boolean isCreate, String method)
/*     */     throws DavException, IOException
/*     */   {
/*  76 */     List<Element> set = Lists.newArrayList();
/*  77 */     List<Element> remove = Lists.newArrayList();
/*  78 */     if (top == null) {
/*  79 */       return null;
/*     */     }
/*  81 */     for (Object obj : top.elements()) {
/*  82 */       if ((obj instanceof Element))
/*     */       {
/*     */ 
/*  85 */         Element e = (Element)obj;
/*  86 */         String nodeName = e.getName();
/*     */         boolean isSet;
/*  88 */         if (nodeName.equals("set")) {
/*  89 */           isSet = true;
/*  90 */         } else { if ((!nodeName.equals("remove")) || 
/*  91 */             (isCreate)) {
/*     */             continue;
/*     */           }
/*  94 */           isSet = false;
/*     */         }
/*     */         
/*     */ 
/*  98 */         e = e.element(DavElements.E_PROP);
/*  99 */         if (e == null) {
/* 100 */           throw new DavException("missing <D:prop> in " + method, 400, null);
/*     */         }
/*     */         
/* 103 */         for (Object propObj : e.elements())
/* 104 */           if ((propObj instanceof Element)) {
/* 105 */             Element propElem = (Element)propObj;
/* 106 */             if (isSet) {
/* 107 */               set.add(propElem);
/*     */             } else
/* 109 */               remove.add(propElem);
/*     */           }
/*     */       }
/*     */     }
/*     */     boolean isSet;
/* 114 */     return new Pair(set, remove);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Pair<List<Element>, List<QName>> processSetsAndRemoves(DavContext ctxt, DavResource resource, List<Element> setElems, List<Element> removeElems, boolean isCreate)
/*     */     throws DavException, IOException
/*     */   {
/* 126 */     List<Element> set = Lists.newArrayList();
/* 127 */     List<QName> remove = Lists.newArrayList();
/* 128 */     DavContext.RequestProp rp = new DavContext.RequestProp(true);
/* 129 */     ctxt.setResponseProp(rp);
/* 130 */     for (Element propElem : setElems) {
/* 131 */       QName propName = propElem.getQName();
/* 132 */       ResourceProperty prop = resource.getProperty(propName);
/* 133 */       if ((prop == null) || (!prop.isProtected())) {
/* 134 */         set.add(propElem);
/* 135 */         rp.addProp(propElem);
/* 136 */       } else if ((isCreate) && (prop.isAllowSetOnCreate())) {
/* 137 */         set.add(propElem);
/*     */       } else {
/* 139 */         rp.addPropError(propName, new DavException.CannotModifyProtectedProperty(propName));
/*     */       }
/*     */     }
/* 142 */     for (Element propElem : removeElems) {
/* 143 */       QName propName = propElem.getQName();
/* 144 */       ResourceProperty prop = resource.getProperty(propName);
/* 145 */       if ((prop == null) || (!prop.isProtected())) {
/* 146 */         remove.add(propName);
/* 147 */         rp.addProp(propElem);
/*     */       } else {
/* 149 */         rp.addPropError(propName, new DavException.CannotModifyProtectedProperty(propName));
/*     */       }
/*     */     }
/* 152 */     return new Pair(set, remove);
/*     */   }
/*     */   
/*     */   public static void handlePropertyUpdate(DavContext ctxt, Element top, DavResource resource, boolean isCreate, String method)
/*     */     throws DavException, IOException
/*     */   {
/* 158 */     if (top == null) {
/* 159 */       return;
/*     */     }
/* 161 */     Pair<List<Element>, List<Element>> elemPair = getSetsAndRemoves(top, isCreate, method);
/* 162 */     Pair<List<Element>, List<QName>> pair = processSetsAndRemoves(ctxt, resource, (List)elemPair.getFirst(), (List)elemPair.getSecond(), isCreate);
/*     */     
/* 164 */     resource.patchProperties(ctxt, (Collection)pair.getFirst(), (Collection)pair.getSecond());
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/method/PropPatch.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */